(defpackage :login
  (:use :common-lisp :cl-who :ht-simple-ajax :hunchentoot)
  (:export :load-login-page))

(in-package :login)

(hunchentoot:define-easy-handler (login-page :uri "/login") ()
  (cl-who:with-html-output-to-string (*standard-output* nil :prologue t)
    (:html :xmlns "http://www.w3.org/1999/xhtml"
     (:head
      (:title "Login to turnbasedgame.")
;      (princ (generate-prologue *ajax-processor*))
      (:script :type "text/javascript" "
//<![CDATA[ 
function fetchURI(uri, callback) {
  var request;
  if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
  else {
    try { request = new ActiveXObject(\"Msxml2.XMLHTTP\"); } catch (e) {
      try { request = new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch (ee) {
        request = null;
      }}}
  if (!request) alert(\"Browser couldn't make a request object.\");

  request.open('GET', uri, true);
  request.onreadystatechange = function() {
    if (request.readyState != 4) return;
    if (((request.status>=200) && (request.status<300)) || (request.status == 304)) {
      var data = request.responseXML;
      if (callback!=null) { callback(data); }
    }
    else { 
      alert('Error while fetching URI ' + uri);
n    }
  }
  request.send(null);
  delete request;
}

function ajax_call(func, callback, args) {
  var uri = '/ajax/' + encodeURIComponent(func) + '/';
  var i;
  if (args.length > 0) {
    uri += '?'
    for (i = 0; i < args.length; ++i) {
      if (i > 0) { uri += '&' };
      uri += 'arg' + i + '=' + encodeURIComponent(args[i]);
    }
  }
  fetchURI(uri, callback);
}


function loginUserCallback(response) {
    if (response.firstChild.firstChild.nodeValue == \"true\") {
      window.location = '/game';
    }
    else {
      document.getElementById('loginResult').textContent = 'Invalid username or password.';
    }
}

function ajax_loginUser (username, password, callback) {
    ajax_call('LOGIN-USER', callback, [username, password]);
}

function loginUser() {
  ajax_loginUser(document.getElementById('username').value, document.getElementById('password').value, loginUserCallback);
  if (document.getElementById('rememberLogin').checked) {
     localStorage.tbgRememberedUsername = document.getElementById('username').value;
  }
  else {
     localStorage.tbgRememberedUsername = '';
  }
    
}

function clearLoginResult() {
   document.getElementById('loginResult').textContent = '';
}

function loadUsername() {
   if (localStorage.tbgRememberedUsername) {
      document.getElementById('username').value = localStorage.tbgRememberedUsername;
      document.getElementById('rememberLogin').checked = true;
  }   
}

"))
     (:body :onload "loadUsername();"
      "
       <form>
       Username: <input type=\"text\" id=\"username\" onChange='clearLoginResult()'/> 
      
       <br/>
       Password: <input type=\"password\" id=\"password\" onChange='clearLoginResult()'/> 
       <br />
       <input type='checkbox' id='rememberLogin'/>Remember username
       <br/>
       <input type=\"button\" id='submitLogin' value=\"Login\" onClick=\"loginUser()\"/>
       <font color = 'red'><span id='loginResult'></span></font>
       </form>"
  ))))